<?php

if (!defined('BASEPATH')) 
	exit('No direct script access allowed');

define("EMPTY", " - ");
class Inbox extends CI_Controller {

	//Doctrine EntityManager
	public $em;

	function __construct() {
		parent::__construct();

		//Not required if you autoload the library
		$this -> load -> library('doctrine');

		//Instantiate a Doctrine Entity Manager
		$this -> em = $this -> doctrine -> em;
		// $conn = $this->em->getConnection();
		//var_dump(models\service\CurrentUser::user());
		if (models\service\CurrentUser::user() == FALSE){
			header('location: ' . base_url());
		}		
	}
	
	
	public function profile($id){
		$user = models\service\CurrentUser::user();
		$criteria = array('userId' => $user->getUserId());
		$userProfiles = $this->em->getRepository('\models\entities\UserProfile')->findBy($criteria);
		// var_dump(isPermitt($userProfiles, $id));die();
		if (isPermitt($userProfiles, $id) === FALSE){
			if (count($userProfiles) > 0){
				//$this->session->set_userdata('flash_message','Anda tidak diijinkan mengakses halaman ini');	
				header('location: ' . base_url() . 'inbox/profile/'.$userProfiles[0]->getProfile()->getId());	
			} else {
				header('location: ' . base_url() . 'report/rekapLaporan');	
			}							
		} 
		$criteria = array('id' => $id);
		$profile = $this->em->getRepository('\models\entities\Profile')->findOneBy($criteria); 
		$link = "";
		if ($id != 6 ){
			$link = "dashboard/manager";
		} else {
			$link = "inbox/profile/".$id;
		}
		$data['breadcrumb'] = array( array("label" => "Home", "link" => base_url() . $link),
								 array("label" => $profile->getMenuInbox(), "link" => NULL)
								);
								
		$serv = new models\service\BerkasService;
					   
		// default search bar state
		$searchBar['fieldnames'] = array( array('label' => 'Nama Perpustakaan',
												'value' => 'Berkas.Perpustakaan.nama'
												),
										  array('label' => 'Nama Indikator',
												'value' => 'Berkas.Indicator.nama'
												)		
										);								
		$pFieldName[0] = "Berkas.Perpustakaan.nama";
		$pFieldValue[0] = "";
		$pCondition[0] = "LIKE";
		$searchBy = FALSE;
		// end default search bar state										
		if ($this->input->post('searchPar') != NULL){			
			$pFieldName=$this->input->post('fieldName');
			$pFieldValue=$this->input->post('fieldValue');
			$pCondition=$this->input->post('condition');
			
			if ($pFieldValue[0] != NULL){
				$searchBy = TRUE;
			}
		}
		
		$searchBar['pFieldName'] = $pFieldName;
		$searchBar['pCondition'] = $pCondition;
		$searchBar['pFieldValue'] = $pFieldValue;
		$data['searchBar'] = $searchBar;
		
		// default paging bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 10;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		if ($this->input->post('activePage') != NULL){
			$indexAndCount[0] = $this->input->post('activePage');			
		}
		
		if ($this->input->post('activePageBar') != NULL){
			$indexAndCount[0] = $this->input->post('activePageBar');
		} 
		// end default paging bar state
		//$user = models\service\CurrentUser::user();
		
		if ($searchBy === TRUE){
			/*$i = count($searchBar['pFieldName']);
			$searchBar['pFieldName'][$i] = "Inbox.procedureStep";
			$searchBar['pCondition'][$i] = "EQUAL";
			$searchBar['pFieldValue'][$i] = $ps[0]->getId();
			
			if ($ps[0]->getId() == 1){
				$searchBar['pFieldName'][$i+1] = "Berkas.reporter";
				$searchBar['pCondition'][$i+1] = "EQUAL";
				$searchBar['pFieldValue'][$i+1] = $user->getUserId();
			}
			
			$data['pBerkas'] = $serv->paginateBerkasByMap($searchBar, $indexAndCount, $orders); */
		} else {
			$criteria = array('id' => $id);
			$profile = $this->em->getRepository('\models\entities\Profile')->findOneBy($criteria);
			$criteria = array('profile' => $profile);
			$prosedurStep = $this->em->getRepository('\models\entities\ProcedureStep')->findBy($criteria);
			if (count($prosedurStep)>0){
				$searchBar['pFieldName'][0] = "Inbox.procedureStep.id";
				$searchBar['pCondition'][0] = "IN";
				foreach ($prosedurStep as $row){
					$ps[] = $row->getId();
				}
				$searchBar['pFieldValue'][0] = implode(",", $ps);
			} else {
				$searchBar['pFieldName'][0] = "Inbox.procedureStep";
				$searchBar['pCondition'][0] = "EQUAL";
				$searchBar['pFieldValue'][0] = $prosedurStep[0];	
			}
			
			// JIKA Profile adalah *CREATE*
			if ($id == 6 || $id == 10){
			/*	echo "<pre>"; print_r($prosedurStep);
				die($prosedurStep[0]->getId()); */
				$searchBar['pFieldName'][1] = "Berkas.reporter";
				$searchBar['pCondition'][1] = "EQUAL";
				$searchBar['pFieldValue'][1] = $user->getUserId();	
			}			
			if ($id == 8){
				$criteria = array('userId' => $user);
				$usercboMapDistriks = $this->em->getRepository('\models\entities\UsercboMapDistrik')->findBy($criteria);
				if (count($usercboMapDistriks)>0){
					$distriksIds = array();
					foreach ($usercboMapDistriks as $row){
						$distriksIds[] = $row->getDistrik()->getId();
					}
					$desaMaps = $this->em->getRepository('\models\entities\Desa')->findByDistrikIds($distriksIds);
					
					if ($desaMaps != NULL){
						foreach ($desaMaps as $row){
							$ds[] = $row->getId();
						}	
						$searchBar['pFieldName'][1] = "Perpustakaan.desa.id";
						$searchBar['pCondition'][1] = "IN";
						$searchBar['pFieldValue'][1] = implode(",", $ds);
					}
				} else {
					$searchBar['pFieldName'][1] = "Perpustakaan.desa.id";
					$searchBar['pCondition'][1] = "EQUAL";
					$searchBar['pFieldValue'][1] = "0";
				}
			}						 			
			$data['pBerkas'] = $serv->paginateBerkasByMap($searchBar, $indexAndCount, $orders);
				
		}		
		
		$data['userProfiles'] = $userProfiles;
		$data['selectedProfile'] = $id;
		$data['user'] = $user;
		if ($data['selectedProfile'] == 6 || $data['selectedProfile'] == 10){
			$this -> load -> view('berkas/index', $data);		
		} else {
			$this -> load -> view('berkas/indexVer', $data);
		}
	}

	public function dashboard(){
		$user = models\service\CurrentUser::user();
		$criteria = array('userId' => $user->getUserId());
		$userProfiles = $this->em->getRepository('\models\entities\UserProfile')->findBy($criteria);
		
	/*	if (isPermitt($userProfiles, $id) === FALSE){
			if (count($userProfiles) > 0){
				header('location: ' . base_url() . 'inbox/profile/'.$userProfiles[0]->getProfile()->getId());	
			} else {
				header('location: ' . base_url() . 'report/rekapLaporan');	
			}							
		}  */
		// $criteria = array('id' => $id);
		// $profile = $this->em->getRepository('\models\entities\Profile')->findOneBy($criteria); 
		$data['breadcrumb'] = array( array("label" => "Home", "link" => base_url() . "inbox/dashboard")
								);
		$data['userProfiles'] = $userProfiles;
		$data['user'] = $user; 						
		// if ($data['selectedProfile'] == 6 || $data['selectedProfile'] == 10){
			// $this -> load -> view('berkas/sentBerkas', $data);		
		// } else {
			// $this -> load -> view('berkas/sentBerkasVer', $data);
		// }
		$this->load->view('berkas/dashboard', $data);						
	}

	public function sentBerkas($id){
		$user = models\service\CurrentUser::user();
		$criteria = array('userId' => $user->getUserId());
		$userProfiles = $this->em->getRepository('\models\entities\UserProfile')->findBy($criteria);
		
		if (isPermitt($userProfiles, $id) === FALSE){
			if (count($userProfiles) > 0){
				header('location: ' . base_url() . 'inbox/profile/'.$userProfiles[0]->getProfile()->getId());	
			} else {
				header('location: ' . base_url() . 'report/rekapLaporan');	
			}							
		} 
		$criteria = array('id' => $id);
		$profile = $this->em->getRepository('\models\entities\Profile')->findOneBy($criteria); 
		$data['breadcrumb'] = array( array("label" => "Home", "link" => base_url() . "inbox/profile/".$id),
								 array("label" => $profile->getMenuInbox(), "link" => NULL)
								);
								
		$serv = new models\service\BerkasService;
					   
		// default search bar state
		$searchBar['fieldnames'] = array( array('label' => 'Nama Perpustakaan',
												'value' => 'Berkas.Perpustakaan.nama'
												),
										  array('label' => 'Nama Indikator',
												'value' => 'Berkas.Indicator.nama'
												)		
										);								
		$pFieldName[0] = "Berkas.Perpustakaan.nama";
		$pFieldValue[0] = "";
		$pCondition[0] = "LIKE";
		$searchBy = FALSE;
		// end default search bar state										
		if ($this->input->post('searchPar') != NULL){			
			$pFieldName=$this->input->post('fieldName');
			$pFieldValue=$this->input->post('fieldValue');
			$pCondition=$this->input->post('condition');
			
			if ($pFieldValue[0] != NULL){
				$searchBy = TRUE;
			}
		}
		
		$searchBar['pFieldName'] = $pFieldName;
		$searchBar['pCondition'] = $pCondition;
		$searchBar['pFieldValue'] = $pFieldValue;
		$data['searchBar'] = $searchBar;
		
		// default paging bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 10;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		if ($this->input->post('activePage') != NULL){
			$indexAndCount[0] = $this->input->post('activePage');			
		}
		
		if ($this->input->post('activePageBar') != NULL){
			$indexAndCount[0] = $this->input->post('activePageBar');
		} 
		// end default paging bar state
		//$user = models\service\CurrentUser::user();
		
		if ($searchBy === TRUE){
			
		} else {
			$criteria = array('id' => $id);
			$profile = $this->em->getRepository('\models\entities\Profile')->findOneBy($criteria);
			$criteria = array('profile' => $profile);
			$prosedurStep = $this->em->getRepository('\models\entities\ProcedureStep')->findBy($criteria);
			if (count($prosedurStep)>0){
				$searchBar['pFieldName'][0] = "Inbox.procedureStep.id";
				$searchBar['pCondition'][0] = ">";
				$lowPs = $prosedurStep[0]->getId();
				foreach ($prosedurStep as $row){
					if ($lowPs > $row->getId()){
						$lowPs = $row->getId();	
					}					
				}
				//$searchBar['pFieldValue'][0] = implode(",", $ps);
				$searchBar['pFieldValue'][0] = $lowPs;
			} else {
				$searchBar['pFieldName'][0] = "Inbox.procedureStep";
				$searchBar['pCondition'][0] = ">";
				$searchBar['pFieldValue'][0] = $prosedurStep[0];	
			}
			
			// JIKA Profile adalah *CREATE*
			if ($id == 6 || $id == 10){
			/*	echo "<pre>"; print_r($prosedurStep);
				die($prosedurStep[0]->getId()); */
				$searchBar['pFieldName'][1] = "Berkas.reporter";
				$searchBar['pCondition'][1] = "EQUAL";
				$searchBar['pFieldValue'][1] = $user->getUserId();	
			}			
			if ($id == 8){
				$criteria = array('userId' => $user);
				$usercboMapDistriks = $this->em->getRepository('\models\entities\UsercboMapDistrik')->findBy($criteria);
				if (count($usercboMapDistriks)>0){
					$distriksIds = array();
					foreach ($usercboMapDistriks as $row){
						$distriksIds[] = $row->getDistrik()->getId();
					}
					$desaMaps = $this->em->getRepository('\models\entities\Desa')->findByDistrikIds($distriksIds);
					
					if ($desaMaps != NULL){
						foreach ($desaMaps as $row){
							$ds[] = $row->getId();
						}	
						$searchBar['pFieldName'][1] = "Perpustakaan.desa.id";
						$searchBar['pCondition'][1] = "IN";
						$searchBar['pFieldValue'][1] = implode(",", $ds);
					}
				} else {
					$searchBar['pFieldName'][1] = "Perpustakaan.desa.id";
					$searchBar['pCondition'][1] = "EQUAL";
					$searchBar['pFieldValue'][1] = "0";
				}
			}						 			
			$data['pBerkas'] = $serv->paginateBerkasByMap($searchBar, $indexAndCount, $orders);
				
		}		
		
		$data['userProfiles'] = $userProfiles;
		$data['selectedProfile'] = $id;
		$data['user'] = $user;
		if ($data['selectedProfile'] == 6 || $data['selectedProfile'] == 10){
			$this -> load -> view('berkas/sentBerkas', $data);		
		} else {
			$this -> load -> view('berkas/sentBerkasVer', $data);
		}
	}

	public function findFinalBerkasByUserLogin(){
		$user = models\service\CurrentUser::user();
		$criteria = array('userId' => $user->getUserId());
		$userProfiles = $this->em->getRepository('\models\entities\UserProfile')->findBy($criteria);
		
		$serv = new models\service\BerkasService;
					   
		// default search bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 10000000;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Berkas.result";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = "FINISH";
			
			// JIKA Profile adalah *CREATE*
			$searchBar['pFieldName'][1] = "Berkas.reporter";
			$searchBar['pCondition'][1] = "EQUAL";
			$searchBar['pFieldValue'][1] = $user->getUserId();	
									 			
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			//echo "<pre>"; print_r($pBerkas); die();
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				//echo $dt->getId()."<br>";
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['tglSelesai'] = $dt->getEndDate()->format('d-m-Y');
				$berkas[$i]['distrik'] = $dt->getPerpustakaan()->getDesa()->getDistrik()->getNama();
				if ($dt->getTipe()->getId() == "TRAINING"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasTraining()->getJenisTraining()->getNama(); 
				} else if ($dt->getTipe()->getId() == "LKPM"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasLkpm()->getJenisKegiatan()->getNama();
				} else if ($dt->getTipe()->getId() == "LAS"){
	    	    	if ($dt->getBerkasLas()->getJenisAsistensi() == 1){
							$pre = "NON KUNJUNGAN";
					} else {
							$pre = "KUNJUNGAN";
					}
					$berkas[$i]['rincianLaporan'] = $pre . " - " . $dt->getBerkasLas()->getKategoriAsistensi()->getNama();
				}
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}

	public function findFinalBerkas(){
		$serv = new models\service\BerkasService;
					   
		// default search bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 10000000;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		// if ($this->input->post('page') != NULL){
			// $indexAndCount[0] = $this->input->post('page');			
		// }
// 		
		// if ($this->input->post('activePageBar') != NULL){
			// $indexAndCount[0] = $this->input->post('activePageBar');
		// } 
		// end default paging bar state
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Berkas.result";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = "FINISH";
			
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['tglSelesai'] = $dt->getEndDate()->format('d-m-Y');
				$berkas[$i]['distrik'] = $dt->getPerpustakaan()->getDesa()->getDistrik()->getNama();
				if ($dt->getTipe()->getId() == "TRAINING"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasTraining()->getJenisTraining()->getNama(); 
				} else if ($dt->getTipe()->getId() == "LKPM"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasLkpm()->getJenisKegiatan()->getNama();
				} else if ($dt->getTipe()->getId() == "LAS"){

    	    if ($dt->getBerkasLas()->getJenisAsistensi() == 1){
						$pre = "NON KUNJUNGAN";
					} else {
						$pre = "KUNJUNGAN";
					}
					$berkas[$i]['rincianLaporan'] = $pre . " - " . $dt->getBerkasLas()->getKategoriAsistensi()->getNama();
				}
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}

	public function findCFBerkas(){
		$serv = new models\service\BerkasService;
					   
		// default search bar state
		$indexAndCount[0] = 1;
		// $indexAndCount[1] = 10;
		$indexAndCount[1] = 10000;  // set unlimited
		$orders = array(array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC'));
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Inbox.procedureStep.id";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = "1";
			
			$searchBar['pFieldName'][1] = "Inbox.condition";
			$searchBar['pCondition'][1] = "EQUAL";
			$searchBar['pFieldValue'][1] = $this->input->post('status');
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['status'] = $dt->getInbox()->getCondition();
				$berkas[$i]['distrik'] = $dt->getPerpustakaan()->getDesa()->getDistrik()->getNama();
				//$berkas[$i]['tglSelesai'] = $dt->getEndDate()->format('d-m-Y');
				$berkas[$i]['rincianLaporan'] = '';
				if ($dt->getTipe()->getId() == "TRAINING"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasTraining()->getJenisTraining()->getNama(); 
				} else if ($dt->getTipe()->getId() == "LKPM"){
					$berkas[$i]['rincianLaporan'] = ($dt->getBerkasLkpm()->getJenisKegiatan() != NULL ? $dt->getBerkasLkpm()->getJenisKegiatan()->getNama() : "");
				} else if ($dt->getTipe()->getId() == "LAS"){
					if ($dt->getBerkasLas()->getJenisAsistensi() == 1){
						$pre = "NON KUNJUNGAN";
					} else {
						$pre = "KUNJUNGAN";
					}
					$berkas[$i]['rincianLaporan'] = $pre . " - " . ($dt->getBerkasLas()->getKategoriAsistensi() != NULL ?  $dt->getBerkasLas()->getKategoriAsistensi()->getNama() : "");
				}
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}

	public function findCFBerkasUser(){
		$serv = new models\service\BerkasService;
		$user = models\service\CurrentUser::user();
					   
		// default search bar state
		$indexAndCount[0] = 1;
		// $indexAndCount[1] = 10;
		$indexAndCount[1] = 10000;  // set unlimited
		$orders = array(array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC'));
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Inbox.procedureStep.id";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = "1";
			
			$searchBar['pFieldName'][1] = "Berkas.reporter";
			$searchBar['pCondition'][1] = "EQUAL";
			$searchBar['pFieldValue'][1] = $user->getUserId();
			
			$searchBar['pFieldName'][2] = "Inbox.condition";
			$searchBar['pCondition'][2] = "EQUAL";
			$searchBar['pFieldValue'][2] = $this->input->post('status');
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['status'] = $dt->getInbox()->getCondition();
				$berkas[$i]['distrik'] = $dt->getPerpustakaan()->getDesa()->getDistrik()->getNama();
				$berkas[$i]['rincianLaporan'] = '';
				if ($dt->getTipe()->getId() == "TRAINING"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasTraining()->getJenisTraining()->getNama(); 
				} else if ($dt->getTipe()->getId() == "LKPM"){
					$berkas[$i]['rincianLaporan'] = ($dt->getBerkasLkpm()->getJenisKegiatan() != NULL ? $dt->getBerkasLkpm()->getJenisKegiatan()->getNama() : "");
				} else if ($dt->getTipe()->getId() == "LAS"){
					if ($dt->getBerkasLas()->getJenisAsistensi() == 1){
						$pre = "NON KUNJUNGAN";
					} else {
						$pre = "KUNJUNGAN";
					}
					$berkas[$i]['rincianLaporan'] = $pre . " - " . ($dt->getBerkasLas()->getKategoriAsistensi() != NULL ?  $dt->getBerkasLas()->getKategoriAsistensi()->getNama() : "");
				}
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}
	
	public function findCFBerkasProgress(){
		$serv = new models\service\BerkasService;
		$user = models\service\CurrentUser::user();
					   
		// default search bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 100000;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		if ($this->input->post('page') != NULL){
			$indexAndCount[0] = $this->input->post('page');			
		}
		
		if ($this->input->post('activePageBar') != NULL){
			$indexAndCount[0] = $this->input->post('activePageBar');
		} 
		// end default paging bar state
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Berkas.reporter";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = $user->getUserId();
			$searchBar['pFieldName'][1] = "Inbox.procedureStep.id";
			$searchBar['pCondition'][1] = "IN";
			$searchBar['pFieldValue'][1] = "2,3,9";
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['status'] = $dt->getInbox()->getCondition();
				$berkas[$i]['tglInbox'] = $dt->getInbox()->getCreatedDate()->format('d-m-Y');
				$berkas[$i]['distrik'] = $dt->getPerpustakaan()->getDesa()->getDistrik()->getNama();
				$berkas[$i]['rincianLaporan'] = '';
				$step = "-";
				if ($dt->getInbox()->getProcedureStep()->getId() == 2){
					$step = "DQA";
				} else if ($dt->getInbox()->getProcedureStep()->getId() == 3){
					$step = "CBO";
				} else if ($dt->getInbox()->getProcedureStep()->getId() == 9){
					$step = "ME";
				}
				
				$berkas[$i]['step'] = $step;
				if ($dt->getTipe()->getId() == "TRAINING"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasTraining()->getJenisTraining()->getNama(); 
				} else if ($dt->getTipe()->getId() == "LKPM"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasLkpm()->getJenisKegiatan()->getNama();
				} else if ($dt->getTipe()->getId() == "LAS"){
					if ($dt->getBerkasLas()->getJenisAsistensi() == 1){
						$pre = "NON KUNJUNGAN";
					} else {
						$pre = "KUNJUNGAN";
					}
					$berkas[$i]['rincianLaporan'] = $pre . " - " . $dt->getBerkasLas()->getKategoriAsistensi()->getNama();
				}
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}

	public function findDQABerkas(){
		$serv = new models\service\BerkasService;
					   
		// default search bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 100000;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		if ($this->input->post('page') != NULL){
			$indexAndCount[0] = $this->input->post('page');			
		}
		
		if ($this->input->post('activePageBar') != NULL){
			$indexAndCount[0] = $this->input->post('activePageBar');
		} 
		// end default paging bar state
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Inbox.procedureStep.id";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = "2";
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['status'] = $dt->getInbox()->getCondition();
				$berkas[$i]['tglInbox'] = $dt->getInbox()->getCreatedDate()->format('d-m-Y');
				$berkas[$i]['distrik'] = $dt->getPerpustakaan()->getDesa()->getDistrik()->getNama();
				$berkas[$i]['rincianLaporan'] = '';
				if ($dt->getTipe()->getId() == "TRAINING"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasTraining()->getJenisTraining()->getNama(); 
				} else if ($dt->getTipe()->getId() == "LKPM"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasLkpm()->getJenisKegiatan()->getNama();
				} else if ($dt->getTipe()->getId() == "LAS"){
					if ($dt->getBerkasLas()->getJenisAsistensi() == 1){
						$pre = "NON KUNJUNGAN";
					} else {
						$pre = "KUNJUNGAN";
					}
					$berkas[$i]['rincianLaporan'] = $pre . " - " . $dt->getBerkasLas()->getKategoriAsistensi()->getNama();
				}
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}
	
	public function findCBOUserBerkas(){
		$serv = new models\service\BerkasService;
		$user = models\service\CurrentUser::user();
					   
		// default search bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 100000;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		if ($this->input->post('page') != NULL){
			$indexAndCount[0] = $this->input->post('page');			
		}
		
		if ($this->input->post('activePageBar') != NULL){
			$indexAndCount[0] = $this->input->post('activePageBar');
		} 
		// end default paging bar state
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Inbox.procedureStep.id";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = "3";
			$criteria = array('userId' => $user);
			$usercboMapDistriks = $this->em->getRepository('\models\entities\UsercboMapDistrik')->findBy($criteria);
			if (count($usercboMapDistriks)>0){
				$distriksIds = array();
				foreach ($usercboMapDistriks as $row){
					$distriksIds[] = $row->getDistrik()->getId();
				}
				$desaMaps = $this->em->getRepository('\models\entities\Desa')->findByDistrikIds($distriksIds);
					
				if ($desaMaps != NULL){
					foreach ($desaMaps as $row){
							$ds[] = $row->getId();
					}	
				$searchBar['pFieldName'][1] = "Perpustakaan.desa.id";
				$searchBar['pCondition'][1] = "IN";
				$searchBar['pFieldValue'][1] = implode(",", $ds);
				}
			} else {
					$searchBar['pFieldName'][1] = "Perpustakaan.desa.id";
					$searchBar['pCondition'][1] = "EQUAL";
					$searchBar['pFieldValue'][1] = "0";
			}
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['status'] = $dt->getInbox()->getCondition();
				$berkas[$i]['tglInbox'] = $dt->getInbox()->getCreatedDate()->format('d-m-Y');
				$berkas[$i]['distrik'] = $dt->getPerpustakaan()->getDesa()->getDistrik()->getNama();
				if ($dt->getTipe()->getId() == "TRAINING"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasTraining()->getJenisTraining()->getNama(); 
				} else if ($dt->getTipe()->getId() == "LKPM"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasLkpm()->getJenisKegiatan()->getNama();
				} else if ($dt->getTipe()->getId() == "LAS"){
					if ($dt->getBerkasLas()->getJenisAsistensi() == 1){
						$pre = "NON KUNJUNGAN";
					} else {
						$pre = "KUNJUNGAN";
					}
					$berkas[$i]['rincianLaporan'] = $pre . " - " . $dt->getBerkasLas()->getKategoriAsistensi()->getNama();
				}
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
			$result['user'] = $user->getUserId();
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}


	public function findCBOBerkas(){
		$serv = new models\service\BerkasService;
					   
		// default search bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 1000000;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		if ($this->input->post('page') != NULL){
			$indexAndCount[0] = $this->input->post('page');			
		}
		
		if ($this->input->post('activePageBar') != NULL){
			$indexAndCount[0] = $this->input->post('activePageBar');
		} 
		// end default paging bar state
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Inbox.procedureStep.id";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = "3";
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['status'] = $dt->getInbox()->getCondition();
				$berkas[$i]['tglInbox'] = $dt->getInbox()->getCreatedDate()->format('d-m-Y');
				$berkas[$i]['distrik'] = $dt->getPerpustakaan()->getDesa()->getDistrik()->getNama();
				if ($dt->getTipe()->getId() == "TRAINING"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasTraining()->getJenisTraining()->getNama(); 
				} else if ($dt->getTipe()->getId() == "LKPM"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasLkpm()->getJenisKegiatan()->getNama();
				} else if ($dt->getTipe()->getId() == "LAS"){
					if ($dt->getBerkasLas()->getJenisAsistensi() == 1){
						$pre = "NON KUNJUNGAN";
					} else {
						$pre = "KUNJUNGAN";
					}
					$berkas[$i]['rincianLaporan'] = $pre . " - " . $dt->getBerkasLas()->getKategoriAsistensi()->getNama();
				}
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}

	public function findCBOUserBerkas2(){
		$serv = new models\service\BerkasService;
		// $user = models\service\CurrentUser::user();
		$user = $this->input->post('userid');
		// default search bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 100000;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		if ($this->input->post('page') != NULL){
			$indexAndCount[0] = $this->input->post('page');			
		}
		
		if ($this->input->post('activePageBar') != NULL){
			$indexAndCount[0] = $this->input->post('activePageBar');
		} 
		// end default paging bar state
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Inbox.procedureStep.id";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = "3";
			$criteria = array('userId' => $user);
			$usercboMapDistriks = $this->em->getRepository('\models\entities\UsercboMapDistrik')->findBy($criteria);
			if (count($usercboMapDistriks)>0){
				$distriksIds = array();
				foreach ($usercboMapDistriks as $row){
					$distriksIds[] = $row->getDistrik()->getId();
				}
				$desaMaps = $this->em->getRepository('\models\entities\Desa')->findByDistrikIds($distriksIds);
					
				if ($desaMaps != NULL){
					foreach ($desaMaps as $row){
							$ds[] = $row->getId();
					}	
				$searchBar['pFieldName'][1] = "Perpustakaan.desa.id";
				$searchBar['pCondition'][1] = "IN";
				$searchBar['pFieldValue'][1] = implode(",", $ds);
				}
			} else {
					$searchBar['pFieldName'][1] = "Perpustakaan.desa.id";
					$searchBar['pCondition'][1] = "EQUAL";
					$searchBar['pFieldValue'][1] = "0";
			}
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['status'] = $dt->getInbox()->getCondition();
				$berkas[$i]['tglInbox'] = $dt->getInbox()->getCreatedDate()->format('d-m-Y');
				$berkas[$i]['distrik'] = $dt->getPerpustakaan()->getDesa()->getDistrik()->getNama();
				if ($dt->getTipe()->getId() == "TRAINING"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasTraining()->getJenisTraining()->getNama(); 
				} else if ($dt->getTipe()->getId() == "LKPM"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasLkpm()->getJenisKegiatan()->getNama();
				} else if ($dt->getTipe()->getId() == "LAS"){
					if ($dt->getBerkasLas()->getJenisAsistensi() == 1){
						$pre = "NON KUNJUNGAN";
					} else {
						$pre = "KUNJUNGAN";
					}
					$berkas[$i]['rincianLaporan'] = $pre . " - " . $dt->getBerkasLas()->getKategoriAsistensi()->getNama();
				}
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}

	public function findMEBerkas(){
		$serv = new models\service\BerkasService;
					   
		// default search bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 1000000;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		if ($this->input->post('page') != NULL){
			$indexAndCount[0] = $this->input->post('page');			
		}
		
		if ($this->input->post('activePageBar') != NULL){
			$indexAndCount[0] = $this->input->post('activePageBar');
		} 
		// end default paging bar state
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Inbox.procedureStep.id";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = "9";
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['status'] = $dt->getInbox()->getCondition();
				$berkas[$i]['tglInbox'] = $dt->getInbox()->getCreatedDate()->format('d-m-Y');
				$berkas[$i]['distrik'] = $dt->getPerpustakaan()->getDesa()->getDistrik()->getNama();
				if ($dt->getTipe()->getId() == "TRAINING"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasTraining()->getJenisTraining()->getNama(); 
				} else if ($dt->getTipe()->getId() == "LKPM"){
					$berkas[$i]['rincianLaporan'] = $dt->getBerkasLkpm()->getJenisKegiatan()->getNama();
				} else if ($dt->getTipe()->getId() == "LAS"){
					if ($dt->getBerkasLas()->getJenisAsistensi() == 1){
						$pre = "NON KUNJUNGAN";
					} else {
						$pre = "KUNJUNGAN";
					}
					$berkas[$i]['rincianLaporan'] = $pre . " - " . $dt->getBerkasLas()->getKategoriAsistensi()->getNama();
				}
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}

	public function findSentBerkasByUserLogin(){
		$user = models\service\CurrentUser::user();
		$criteria = array('userId' => $user->getUserId());
		$userProfiles = $this->em->getRepository('\models\entities\UserProfile')->findBy($criteria);
		
		$serv = new models\service\BerkasService;
					   
		// default search bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 10;
		$orders = array(
						array('fieldName' => 'Berkas.updateDate', 'orderType' => 'DESC')
					   );
		   
		if ($this->input->post('page') != NULL){
			$indexAndCount[0] = $this->input->post('page');			
		}
		
		if ($this->input->post('activePageBar') != NULL){
			$indexAndCount[0] = $this->input->post('activePageBar');
		} 
		// end default paging bar state
		$searchBy = FALSE;
		$result = NULL;
		if ($searchBy === TRUE){
			
		} else {			
			$searchBar['pFieldName'][0] = "Berkas.reporter";
			$searchBar['pCondition'][0] = "EQUAL";
			$searchBar['pFieldValue'][0] = $user->getUserId();
			$profileCreateId = 0;	
			foreach ($userProfiles as $up){
				if ($up->getProfile()->getId() == 6 || $up->getProfile()->getId() == 10){
					$profileCreateId = $up->getProfile()->getId();
					break;	
				}			
			}
			if ($profileCreateId > 0){
				//var_dump($profileCreateId);
				$criteria = array('id' => $profileCreateId);
				$profile = $this->em->getRepository('\models\entities\Profile')->findOneBy($criteria);
				$criteria = array('profile' => $profile);
				$prosedurStep = $this->em->getRepository('\models\entities\ProcedureStep')->findBy($criteria);
				
				$searchBar['pFieldName'][1] = "Inbox.procedureStep.id";
				$searchBar['pCondition'][1] = ">";
				$searchBar['pFieldValue'][1] = $prosedurStep[0]->getId();
			}						 			
			$pBerkas = $serv->paginateBerkasByMap2($searchBar, $indexAndCount, $orders);
			
			$i = 0;
			foreach ($pBerkas['data'] as $dt){
				//echo $dt->getId()."<br>";
				$berkas[$i]['id'] = $dt->getId();
				$berkas[$i]['namaPerpustakaan'] = $dt->getPerpustakaan()->getNama();
				$berkas[$i]['jenisLaporan'] = $dt->getTipe()->getNama();
				$berkas[$i]['tglLaporan'] = $dt->getStartDate()->format('d-m-Y');
				$berkas[$i]['posisi'] = $dt->getInbox()->getProcedureStep()->getNama();
				$berkas[$i]['posisiId'] = $dt->getInbox()->getProcedureStep()->getId();
				//$berkas[$i]['tglSelesai'] = $dt->getEndDate()->format('d-m-Y');
				$i++;				
			}
			if ($i == 0){
				$berkas=NULL;
			}
			$result['data'] = $berkas;
			$result['count'] = $pBerkas['count'];
			$result['indexAndCount'] = $pBerkas['indexAndCount'];
		}		
		header('Content-type: application/json');
		if ($pBerkas == NULL){
			$data['code'] = 0;  
			$data['message'] = "Tidak ditemukan";	
		} else {
			$data['code'] = 1;
			$data['message'] = "Sukses";
			$data['resultData'] = $result;
		}
		echo json_encode($data);
	}
	
	public function createEdit($selectedProfile="0", $id="0"){
		$user = models\service\CurrentUser::user();
		$criteria = array('userId' => $user->getUserId());
		$userProfiles = $this->em->getRepository('\models\entities\UserProfile')->findBy($criteria);
		
		$criteria = array("id" => $id);	
		$berkas = $this -> em -> getRepository('\models\entities\Berkas')->findOneBy($criteria);
		
		$criteria = array("id" => $berkas);	
		$inbox = $this -> em -> getRepository('\models\entities\Inbox')->findOneBy($criteria);
		$isPermitAccessBerkas = false;
		if ($inbox == null && $berkas == null){
			// perlapor edit sendiri
			$isPermitAccessBerkas = true;
		} else {
			if ($inbox->getProcedureStep()->getProfile()->getId() == $selectedProfile){
				$isPermitAccessBerkas = true;
			} else {
				$isPermitAccessBerkas = false;
			}	
		}
		
		if (isPermitt($userProfiles, $selectedProfile) === FALSE || $isPermitAccessBerkas == false){
			if (count($userProfiles) > 0){
				//$this->session->set_userdata('flash_message','Anda tidak diijinkan mengakses halaman ini');	
				header('location: ' . base_url() . 'inbox/profile/'.$userProfiles[0]->getProfile()->getId());	
			} else {
				header('location: ' . base_url() . 'report/rekapLaporan');	
			}							
		}
		$profile = $this -> em -> getRepository('\models\entities\Profile') -> findOneBy(array('id' => $selectedProfile));
		if ($selectedProfile == 6 || $selectedProfile == 10){
			$label = "Tambah Laporan";
		} else {
			$label = "Lihat Laporan";
		}
		$home = base_url().'dashboard/manager';
		if ($selectedProfile == 6){
			$home = NULL;
		}
		$data['breadcrumb'] = array( array("label" => "Home", "link" => $home),
								     array("label" => $profile->getMenuInbox(), "link" => base_url() . "inbox/profile/".$selectedProfile),
									 array("label" => $label, "link" => NULL)
								);
								
		$data['id'] = $id;
		$serv = new models\service\BerkasService;
		if ($id != "0"){
			// init the data
			$berkas = $serv->getBerkasById(TRUE, $id);
			$berkasComment = NULL;
			$berkasTraining = $this -> em -> getRepository('\models\entities\BerkasTraining') -> findOneBy(array('id' => $berkas));
			$berkasLkpm = $this -> em -> getRepository('\models\entities\BerkasLkpm') -> findOneBy(array('id' => $berkas));
			$berkasLas = $this -> em -> getRepository('\models\entities\BerkasLas') -> findOneBy(array('id' => $berkas));
			$memberOfTraining = $this -> em -> getRepository('\models\entities\ParticipantOfTraining') -> findBy(array('berkas' => $berkas));
			$berkasComments = $this -> em -> getRepository('\models\entities\BerkasComment') -> findBy(array('berkas' => $berkas), array('createdDate' => 'ASC'));									
			$berkasAttachments = $this -> em -> getRepository('\models\entities\BerkasAttachment') -> findBy(array('berkas' => $berkas), array('createdDate' => 'ASC'));
			$berkasKontribusis = $this -> em -> getRepository('\models\entities\BerkasKontribusi') -> findBy(array('berkas' => $berkas), array('id' => 'ASC'));
			if ($berkasLkpm != null){
				if ($berkasLkpm->getJenisKegiatan() != NULL){
					$jenisKegiatanList = $this -> em -> getRepository('\models\entities\JenisKegiatan') -> findBy(array('kategori' => $berkasLkpm->getJenisKegiatan()->getKategori()), array('id' => 'ASC'));		
				} else {
					$jenisKegiatanList = null;
				}						
			} else {
				$jenisKegiatanList = null;
			}
			
			$data['perpusList'] = $serv->findAllPerpustakaan(array('nama' => 'asc'));
			$data['desaList'] = $serv->findAllDesa(array('nama' => 'asc'));
		} else {
			$berkas = NULL;
			$berkasComments = NULL;
			$berkasTraining = NULL;
			$berkasLkpm = NULL;
			$berkasLas = NULL;
			$memberOfTraining = NULL;
			$berkasAttachments = NULL;
			$berkasKontribusis = array();
			$berkasLkpm = NULL;
			$jenisKegiatanList = NULL;			
			$data['perpusList'] = NULL;
			$data['desaList'] = NULL;
		}
		//echo "<pre>";print_r($berkas->getInbox()->getProcedureStep()->getId());die(); 
		
		$data['userProfiles'] = $userProfiles;
		$data['berkasAttachments'] = $berkasAttachments;
		$data['berkasKontribusis'] = $berkasKontribusis;
		$data['memberOfTraining'] = $memberOfTraining;
		$data['kategoriKegiatanList'] = $this -> em -> getRepository('\models\entities\KategoriKegiatan') -> findAll(array('id' => 'ASC')); 
		$data['jenisTrainingList'] = $this -> em -> getRepository('\models\entities\JenisTraining') -> findByUserProfiles($userProfiles);
		$data['topikKegiatanList'] = $this -> em -> getRepository('\models\entities\TopikKegiatan') -> findAll(array('id' => 'ASC'));
		$data['tipeBerkasList'] = $this -> em -> getRepository('\models\entities\TipeBerkas') -> findBy(array('status'=> 1));
		$data['kontribusis'] = $this -> em -> getRepository('\models\entities\Kontribusi') -> findAll(array('id' => 'ASC'));
		$data['medias'] = $this -> em -> getRepository('\models\entities\Media') -> findAll(array('id' => 'ASC'));
		$data['kategoriAsistensis'] = $this -> em -> getRepository('\models\entities\KategoriAsistensi') -> findAll(array('id' => 'ASC'));
		$data['jenisKegiatanList'] = $jenisKegiatanList;
		$data['distrikList'] = $serv->findAllDistrik(array('nama' => 'asc'));
		$data['berkas'] = $berkas;
		$data['berkasTraining'] = $berkasTraining;
		$data['berkasLkpm'] = $berkasLkpm;
		$data['berkasLas'] = $berkasLas;
		$data['berkasComments'] = $berkasComments;
		$data['selectedProfile'] = $selectedProfile;
		
		
		$isEnabledSubmitBtn = TRUE;
		if ($inbox!=null && $inbox->getProcedureStep()->getIsParalel() == 1){
			if ($inbox->getProcedureStep()->getVerifyByJumlah() != NULL && $inbox->getProcedureStep()->getVerifyByJumlah()>0){
				if ($inbox->getLastVer() == $user->getUserId()){
					$isEnabledSubmitBtn = FALSE;
				}
			}	
											
			if ($inbox->getProcedureStep()->getVerifyByJabatan() != NULL && $inbox->getProcedureStep()->getVerifyByJabatan() != $user->getJabatan()->getId()){
				$isEnabledSubmitBtn = FALSE;
			}
		}
		$data['isEnabledSubmitBtn'] = $isEnabledSubmitBtn;
		$data['user'] = $user;
		$data['isDqa'] = false;
		if ($selectedProfile == 6 || $selectedProfile == 10){
			
			if ($selectedProfile == 10){
				$data['isDqa'] = true;
			}
			$this->load->view('berkas/createEdit', $data);	
		} else if ($selectedProfile == 11) {
			//$this->load->view('berkas/createEditME', $data);
			$this->load->view('berkas/createEditME', $data);
		} else {
			$this->load->view('berkas/createEditVer', $data);
		}
		
		
		
	}

	public function saveAndSubmitReport(){
		
		if ($this -> input -> post('Berkas_id') != NULL) {
			$objects = array('Berkas');
			$except['Berkas'] = array('reporter','verifier','approver','comments', 'startDate','endDate','result','updateDate');
			$xfields = NULL;
			$jmlPria = 0; $jmlWanita = 0; $jmlStaff=0; $jmlNonStaff=0;$jmlLainnya=0;
			if ($this->input->post('Berkas_tipe') == 'TRAINING'){
				array_push($objects, 'BerkasTraining');
				$except['BerkasTraining'] = array();
				if ($this->input->post('participantName') != NULL){
					$participantNames = $this->input->post('participantName');
					$participantGenders = $this->input->post('participantGender');
					$participantStaffs = $this->input->post('participantStaff');
					$i=0;					
					foreach ($participantNames as $participantName){
						$xfields['MemberOfTraining'][$i]['name'] = $participantName;
						$xfields['MemberOfTraining'][$i]['gender'] = $participantGenders[$i];
						$xfields['MemberOfTraining'][$i]['staff'] = $participantStaffs[$i];
						$i++;
					}
					
					foreach ($xfields['MemberOfTraining'] as $row){
						if ($row['gender'] == "M"){
							$jmlPria++;
						} else {
							$jmlWanita++;
						}
						if ($row['staff'] == "S"){
							$jmlStaff++;
						} else if ($row['staff'] == "N"){
							$jmlNonStaff++;
						} else if ($row['staff'] == "L"){
							$jmlLainnya++;
						}
					}
				}				
			}
			
			$fields = collectingHtmlInput($objects, $except);
			if ($fields != NULL){
				array_push($fields, $xfields);				
			}
			//echo "<pre>";print_r($fields);//die();
			$serv = new models\service\BerkasService;
			
			// HARDCODE!! 			
			$user = models\service\CurrentUser::user();			
			$criteria = array('userId' => $user->getUserId());
			$userProfiles = $this->em->getRepository('\models\entities\UserProfile')->findOneBy($criteria);
			
			$fields['Profile']['id'] = $this->input->post('Profile_id');
			if ($fields['Profile']['id'] != '11'){
				$fields['Berkas']['reporter'] = $user->getUserId();
			}
			if ($this->input->post('Berkas_tipe') == 'TRAINING'){
				$fields['BerkasTraining']['jmlStaff'] = $jmlStaff;
				$fields['BerkasTraining']['jmlNonStaff'] = $jmlNonStaff;
				$fields['BerkasTraining']['jmlLainnya'] = $jmlLainnya;
				$fields['BerkasTraining']['jmlPria'] = $jmlPria;
				$fields['BerkasTraining']['jmlWanita'] = $jmlWanita;
			} else if ($this->input->post('Berkas_tipe') == 'LKPM'){
				$fields['BerkasLkpm']['jumlahPria'] = $this->input->post('BerkasLkpm_jumlahPria');
				$fields['BerkasLkpm']['jumlahWanita'] = $this->input->post('BerkasLkpm_jumlahWanita');
				$fields['BerkasLkpm']['topikKegiatan'] = $this->input->post('BerkasLkpm_topikKegiatan');
				$fields['BerkasLkpm']['jumlahTotal'] = $this->input->post('BerkasLkpm_jumlahTotal');
				$fields['BerkasLkpm']['jenisKegiatan'] = $this->input->post('BerkasLkpm_jenisKegiatan');
				$fields['BerkasLkpm']['fromDate'] = $this->input->post('BerkasLkpm_fromDate');
				$fields['BerkasLkpm']['toDate'] = $this->input->post('BerkasLkpm_toDate');
				if (count($this->input->post('BerkasLkpmSasaran')) > 0){
					$i = 0;
					foreach ($this->input->post('BerkasLkpmSasaran') as $row){
						$fields['BerkasLkpmSasaran'][$i] = $row;
						$i++;						
					}
				}
				$fields['kelompokNonPemerintah'] = $this->input->post('kelompokNonPemerintah');
			} else if ($this->input->post('Berkas_tipe') == 'LAS'){
				$fields['BerkasLas']['media'] = $this->input->post('BerkasLas_media');
				$fields['BerkasLas']['jenisAsistensi'] = $this->input->post('BerkasLas_jenisAsistensi');
				$fields['BerkasLas']['kategoriAsistensi'] = $this->input->post('BerkasLas_kategoriAsistensi');
				$fields['BerkasLas']['fromDate'] = $this->input->post('BerkasLas_fromDate');
				$fields['BerkasLas']['toDate'] = $this->input->post('BerkasLas_toDate');				
			}
			
			$kontribusis = $this->em->getRepository('\models\entities\Kontribusi')->findAll();
			$i=0;
			foreach ($kontribusis as $kontribusi){
				if ($this->input->post('Kontribusi_'.$kontribusi->getNama()) != null){
					$fields['BerkasKontribusi'][$i]['kontribusi'] = $this->input->post('Kontribusi_'.$kontribusi->getNama());
					$fields['BerkasKontribusi'][$i]['jumlah'] = $this->input->post('BerkasKontribusi_'.$kontribusi->getNama().'_jumlah');
					$fields['BerkasKontribusi'][$i]['namaLain'] = $this->input->post('BerkasKontribusi_'.$kontribusi->getNama());
					$i++;
				}
			}
			
			if ($serv->saveAndSubmitBerkas($fields) !== FALSE){
				$this->session->set_userdata('flash_message',MESSAGE_REPORT_SENT_SUCCESS);				
				header('location: ' . base_url() . 'inbox/profile/'.$fields['Profile']['id']);	
			}			
		}
	}

	public function save(){
		// echo "<pre>";print_r($_POST); die();
		// echo "<pre>";print_r($_FILES);die('tes');	
		if ($this -> input -> post('Berkas_id') != NULL) {
			$objects = array('Berkas');
			$except['Berkas'] = array('reporter','verifier','approver','comments', 'startDate','endDate','result','updateDate');
			$xfields = NULL;
			$jmlPria = 0; $jmlWanita = 0; $jmlStaff=0; $jmlNonStaff=0; $jmlLainnya=0;
			if ($this->input->post('Berkas_tipe') == 'TRAINING'){
				array_push($objects, 'BerkasTraining');
				$except['BerkasTraining'] = array();
				if ($this->input->post('participantName') != NULL){
					$participantNames = $this->input->post('participantName');
					$participantGenders = $this->input->post('participantGender');
					$participantStaffs = $this->input->post('participantStaff');
					$i=0;					
					foreach ($participantNames as $participantName){
						$xfields['MemberOfTraining'][$i]['name'] = $participantName;
						$xfields['MemberOfTraining'][$i]['gender'] = $participantGenders[$i];
						$xfields['MemberOfTraining'][$i]['staff'] = $participantStaffs[$i];
						$i++;
					}
					
					foreach ($xfields['MemberOfTraining'] as $row){
						if ($row['gender'] == "M"){
							$jmlPria++;
						} else {
							$jmlWanita++;
						}
						
						if ($row['staff'] == "S"){
							$jmlStaff++;
						} else if ($row['staff'] == "N") {
							$jmlNonStaff++;
						} else {
							$jmlLainnya++;
						}
					}
				}				
			}
			
			$fields = collectingHtmlInput($objects, $except);
			if ($fields != NULL){
				array_push($fields, $xfields);				
			}
			//echo "<pre>";print_r($fields);die();
			$serv = new models\service\BerkasService;
			
			// HARDCODE!! 			
			$user = models\service\CurrentUser::user();			
			$criteria = array('userId' => $user->getUserId());
			$userProfiles = $this->em->getRepository('\models\entities\UserProfile')->findOneBy($criteria);
			
			$fields['Profile']['id'] = $this->input->post('Profile_id');
			$fields['Berkas']['reporter'] = $user->getUserId();
			if ($this->input->post('Berkas_tipe') == 'TRAINING'){
				$fields['BerkasTraining']['jmlStaff'] = $jmlStaff;
				$fields['BerkasTraining']['jmlNonStaff'] = $jmlNonStaff;
				$fields['BerkasTraining']['jmlLainnya'] = $jmlLainnya;
				$fields['BerkasTraining']['jmlPria'] = $jmlPria;
				$fields['BerkasTraining']['jmlWanita'] = $jmlWanita;
			} else if ($this->input->post('Berkas_tipe') == 'LKPM'){
				$fields['BerkasLkpm']['jumlahPria'] = $this->input->post('BerkasLkpm_jumlahPria');
				$fields['BerkasLkpm']['jumlahWanita'] = $this->input->post('BerkasLkpm_jumlahWanita');
				$fields['BerkasLkpm']['topikKegiatan'] = $this->input->post('BerkasLkpm_topikKegiatan');
				$fields['BerkasLkpm']['jumlahTotal'] = $this->input->post('BerkasLkpm_jumlahTotal');
				$fields['BerkasLkpm']['jenisKegiatan'] = $this->input->post('BerkasLkpm_jenisKegiatan');
				$fields['BerkasLkpm']['fromDate'] = $this->input->post('BerkasLkpm_fromDate');
				$fields['BerkasLkpm']['toDate'] = $this->input->post('BerkasLkpm_toDate');
				if (count($this->input->post('BerkasLkpmSasaran')) > 0){
					$i = 0;
					foreach ($this->input->post('BerkasLkpmSasaran') as $row){
						$fields['BerkasLkpmSasaran'][$i] = $row; 
						$i++;
					}
				}
				$fields['kelompokNonPemerintah'] = $this->input->post('kelompokNonPemerintah');
			} else if ($this->input->post('Berkas_tipe') == 'LAS'){
				$fields['BerkasLas']['media'] = $this->input->post('BerkasLas_media');
				$fields['BerkasLas']['jenisAsistensi'] = $this->input->post('BerkasLas_jenisAsistensi');
				$fields['BerkasLas']['kategoriAsistensi'] = $this->input->post('BerkasLas_kategoriAsistensi');
				$fields['BerkasLas']['fromDate'] = $this->input->post('BerkasLas_fromDate');
				$fields['BerkasLas']['toDate'] = $this->input->post('BerkasLas_toDate');				
			}
			
			$kontribusis = $this->em->getRepository('\models\entities\Kontribusi')->findAll();
			$i=0;
			foreach ($kontribusis as $kontribusi){
				if ($this->input->post('Kontribusi_'.$kontribusi->getNama()) != null){
					$fields['BerkasKontribusi'][$i]['kontribusi'] = $this->input->post('Kontribusi_'.$kontribusi->getNama());
					$fields['BerkasKontribusi'][$i]['jumlah'] = $this->input->post('BerkasKontribusi_'.$kontribusi->getNama().'_jumlah');
					$fields['BerkasKontribusi'][$i]['namaLain'] = $this->input->post('BerkasKontribusi_'.$kontribusi->getNama());
					$i++;
				}
			}
			//echo "<pre>"; print_r($fields);die();
			//$res = $serv->saveBerkasAsDraft($fields);
			//var_dump($res);die();
			if ($serv->saveBerkasAsDraft($fields) !== FALSE){
				$this->session->set_userdata('flash_message',MESSAGE_REPORT_SAVE_SUCCESS);				
				header('location: ' . base_url() . 'inbox/profile/'.$fields['Profile']['id']);	
			} else {
				//$this->session->set_userdata('flash_message',MESSAGE_REPORT_SAVE_FAILED);
				header('location: ' . base_url() . 'inbox/profile/'.$fields['Profile']['id']);	
			}			
		} else {
			echo "<pre>";print_r($_POST);
			echo "<pre>";print_r($_FILES);
			die('no parameter');
		}
	}

	public function submit(){
		$id = $this->input->post('Berkas_id');
		//var_dump($id);
		//echo "<pre>"; print_r($_POST);die();
		if ($id != "0"){
			$criteria = array('id' => $id);
			$berkas = $this->em->getRepository('\models\entities\Berkas')->findOneBy($criteria);
			$serv = new models\service\BerkasService;
			$user = models\service\CurrentUser::user();			
			$profileId = $this->input->post('Profile_id');
					
			//echo "<pre>"; print_r($berkas); die();
			$criteria = array('id' => $berkas);
			$inbox = $this->em->getRepository('\models\entities\Inbox')->findOneBy($criteria);
			if ($inbox == null){
				header('location: ' . base_url() . 'inbox/profile/'.$profileId);
			}
			$isParalelFlowBerkasTraining = FALSE;			
			// if ($berkas->getTipe()->getId() == "TRAINING"){
				// $berkasTraining = $this->em->getRepository('\models\entities\BerkasTraining')->findOneBy($criteria);
				//echo $berkasTraining->getJenisTraining()->getJenisFlow();die();
				
				//echo "<pre>"; print_r($inbox); die();
				// HARDCODE !!!
				//if ($berkasTraining->getJenisTraining()->getJenisFlow() == "JP2" && ($inbox->getProcedureStep()->getName() == "CBO_VERIFY_DOC" || $inbox->getProcedureStep()->getId() == 3)){
			//echo "<pre>";print_r($inbox);die($berkas->getId());		
			if ($inbox->getProcedureStep()->getIsParalel() == 1){	
				$isParalelFlowBerkasTraining = TRUE;	
			}	
			// } 			  
			
			$cond = "OK";
			$criteria = array('origin' => $inbox->getProcedureStep());
			$ps = $this->em->getRepository('\models\entities\ProcedureTransition')->findOneBy($criteria);
		
			if ($ps->getCondition() == "FINISH"){
				$cond = "FINISH";
				$berkas->setApprover($user->getUserId());	
			} else {
				$berkas->setVerifier($user->getUserId());	
			}
			/*if ($profileId == 11){
				$cond = "FINISH";
			}*/
			if ($isParalelFlowBerkasTraining){
				/*$criteria = array('userId' => $user);
				$userVerifikator = $this->em->getRepository('\models\entities\UserVerifikator')->findBy($criteria);
				
				$criteria = array('id' => $berkas);
				$inbox = $this->em->getRepository('\models\entities\Inbox')->findOneBy($criteria); */
				$namaJabatan = $user->getJabatan()->getId();
				
				//if ($userVerifikator[0]->getJenisVerifikator()->getId()==2 && $inbox->getJumlahVerify() == 1){
				if ($inbox->getProcedureStep()->getVerifyByJabatan() == $namaJabatan){	//die("kadiyeu");					
					if ($serv->submitBerkasForVerAndApp($berkas, $profileId, $cond) !== FALSE){
						// die("1");
						$this->session->set_userdata('flash_message',MESSAGE_REPORT_SENT_SUCCESS);
						header('location: ' . base_url() . 'inbox/profile/'.$profileId);
					}	
				} else if ($inbox->getProcedureStep()->getVerifyByJumlah() == ($inbox->getJumlahVerify() + 1)){ //die("kadiTu");
					if ($serv->submitBerkasForVerAndApp($berkas, $profileId, $cond) !== FALSE){
						// echo $cond;
						// echo $profileId;
						// die("2");
						$this->session->set_userdata('flash_message',MESSAGE_REPORT_SENT_SUCCESS);
						header('location: ' . base_url() . 'inbox/profile/'.$profileId);
					}
				} else {	//die("kadihela");				
					if ($serv->submitReview($berkas, $profileId, "OK") !== FALSE){
						// die("3");
						$this->session->set_userdata('flash_message',MESSAGE_REPORT_SENT_SUCCESS);
						header('location: ' . base_url() . 'inbox/profile/'.$profileId);
					}					
				}
								
			} else {	//echo $berkasTraining->getJenisTraining()->getJenisFlow();die("not paralel");	
				if ($serv->submitBerkasForVerAndApp($berkas, $profileId, $cond) !== FALSE){
					$this->session->set_userdata('flash_message',MESSAGE_REPORT_SENT_SUCCESS);	
					// die("4");
					header('location: ' . base_url() . 'inbox/profile/'.$profileId);										
				}	
			}
							
		} else {
			die("NOT FOUND");
		}		
	}

	public function returnReportPost(){
		
		$id = $this->input->post('Berkas_id');		
		if ($id != "0"){
			$criteria = array('id' => $id);
			$berkas = $this->em->getRepository('\models\entities\Berkas')->findOneBy($criteria);
			$serv = new models\service\BerkasService;
			$user = models\service\CurrentUser::user();			
			$profileId = $this->input->post('Profile_id');
			  
			if ($serv->submitBerkasForVerAndApp($berkas, $profileId,'NOK') !== FALSE){
				$this->session->set_userdata('flash_message',MESSAGE_REPORT_RETURN_SUCCESS);
				header('location: ' . base_url() . 'inbox/profile/'.$profileId);					
			}				
		} else {
			die("NOT FOUND");
		}
		
	}

	public function viewBerkasAttachment($attachId){
	/*
		$permission = $this->getPermission();
		
		if (!in_array("001", $permission)){
			header('location: ' . base_url() . 'home'); 
		}*/
		$criteria = array('id' => $attachId);
		$attach = $this -> em -> getRepository('\models\entities\BerkasAttachment') -> findOneBy($criteria);
		
		// echo "<pre>"; print_r($_SERVER);die();
		$file_location = $attach->getPath(); // store as constant maybe inside index.php - PDF = 'uploads/pdf/';
		// die($file_location); 
	             //must have PDF viewer installed in browser !
	    $content = file_get_contents($file_location);
	    
		if ($attach->getTypeFile() == "application/pdf"){
			$this->output
	           ->set_content_type('application/pdf')
	           ->set_output($content, $attach->getBerkas()->getId().'.pdf');	
		} else {
			
			if (file_exists($file_location)) {				
			    header('Content-Description: File Transfer');
			    header('Content-Type: application/octet-stream');
			    header('Content-Disposition: attachment; filename='.basename($file_location));
			    header('Expires: 0');
			    header('Cache-Control: must-revalidate');
			    header('Pragma: public');
			    header('Content-Length: ' . filesize($file_location));
			    readfile($file_location);
			    exit;
			}
		}
	    
	}

	public function cancelReport($profileId="0", $id = "0"){
		//$id = $this->input->post('Berkas_id');
		
		if ($id != "0"){
			$criteria = array('id' => $id);
			$berkas = $this->em->getRepository('\models\entities\Berkas')->findOneBy($criteria);
			$serv = new models\service\BerkasService;
			$user = models\service\CurrentUser::user();			
			  
			if ($serv->submitBerkasForVerAndApp($berkas, $profileId,'CANCEL') !== FALSE){
				$this->session->set_userdata('flash_message',MESSAGE_REPORT_DELETE_SUCCESS);
				header('location: ' . base_url() . 'inbox/profile/'.$profileId);								
			}				
		} else {
			die("NOT FOUND");
		}
	}

	public function notifReviewPerDay(){
			
	}
	
	public function getBerkasByType(){
		$serv = new models\service\BerkasService;
		$pBerkas = $serv->getBerkasTotalByType($this->input->post('status'));
		$results = array();
		foreach ($pBerkas as $value) {
			$results[] = array($value['nama'], intval($value['jumlah']));
		}
		echo json_encode($results);
		header('Content-type: application/json');
	}		
}

